package com.gsnd.briuppts.service.impl;

import com.gsnd.briuppts.entity.VO.CountVO;
import com.gsnd.briuppts.mapper.AnalysisMapper;
import com.gsnd.briuppts.mapper.IssueRecordMapper;
import com.gsnd.briuppts.mapper.ManagerAnimalMapper;
import com.gsnd.briuppts.mapper.ManagerDiseaseMapper;
import com.gsnd.briuppts.service.AnalysisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AnalysisServiceImpl implements AnalysisService {
    @Autowired
    AnalysisMapper analysisMapper;
    @Autowired
    ManagerAnimalMapper managerAnimalMapper;
    @Autowired
    IssueRecordMapper issueRecordMapper;
    @Autowired
    ManagerDiseaseMapper managerDiseaseMapper;

    @Override
    public CountVO findAnalysisCount() {
        CountVO countVO=new CountVO();
        String[] name={"栏舍","栏圈","动物","冷库","用户"};
        String[] value=analysisMapper.count();
        countVO.setName(name);
        countVO.setValue(value);
        return countVO;
    }

    @Override
    public Map<String, Integer> countWeight() {
        return managerAnimalMapper.countWeight();
    }

    @Override
    public Map<String, Long> countDisease() {
        List<Map<String, Object>> mapList = managerDiseaseMapper.countDisease();
        HashMap<String, Long> map = new HashMap<>();
        mapList.forEach(e -> {
            map.put(String.valueOf(e.get("d_name")), (Long) e.get("sum"));
        });
        return map;

    }

    @Override
    public Map<String, List<Object>> countSales() {
        // 月份列表
        String[] month = {"1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"};
        List<Object> monthList = Arrays.asList(month);
        // 对应销售总数列表
        Integer[] sales = new Integer[12];
        // 初始化：全0填充
        Arrays.fill(sales, 0);
        List<Object> salesList = Arrays.asList(sales);
        List<Map<String, Integer>> baseData = issueRecordMapper.countSales();
        baseData.forEach(e ->
                salesList.set(e.get("月份") - 1, Integer.parseInt(String.valueOf(e.get("销售总数"))))
        );
        // 创建总结果集map
        HashMap<String, List<Object>> map = new HashMap<>();
        map.put("name", monthList);
        map.put("value", salesList);
        System.out.println(map);
        return map;
    }
}
